<?php

Raise::load('core.ui.RaisePage');
Raise::load('core.text.RaiseText');
Raise::load('core.text.html.RaiseHtml');

/**
 * RaiseDefaultExceptionPage class
 * the default exception page that phpRaise uses when no custom exception page is provided
 *
 * @author Sam Yong
 * @license http://www.opensource.org/licenses/bsd-license New BSD License
 * @package phpraise.core.exception
 * @since 1.2
 */
class RaiseDefaultExceptionPage extends RaisePage {

    /**
     * The exception that is thrown
     * @var RaiseException
     */
    private $exception;

    public function __construct($ex) {
        parent::__construct(array(), '{page.doctype}
<html>
    <head>
        <title>{page.title}</title>
        <style type="text/css"><!--
            body{font: normal 12px/1.5 Tahoma, Verdana, Sans-serif;margin:0px;padding:0px;}
            div.m pre, div.m div.msg{background:#EEE; padding:10px;margin:0px 0px 20px;}
            div.m div.s{font: bold 15px/1.1 Lucida Sans, Tahoma, Sans-serif;padding:0px 0px 5px 10px;}
            div.m{width:600px;margin:20px auto;}
            div.m div.st{padding:5px 0;}
            div.m div.fl{font: normal 11px/1.2 Lucida Sans, Tahoma, San-serif; font-weight:bold;}
        --></style>
    </head>
    <body>
        <div class="m">{default}</div>
    </body>
</html>');
        $this->exception = $ex;
    }
    
    public function header($headers) {
        parent::header($headers);
        
        if ($this->exception instanceof RaisePageNotFoundException) {
            $headers->add('HTTP/1.1 404 Not Found');
        }
    }
    
    public function build() {
        $this->title = 'Application Exception';
        $trace = $this->exception->getTrace();
        $strTrace = '';
        foreach ($trace as $t) {
            $args = array();
            foreach ($t['args'] as $a) {
                if (is_numeric($a)) {
                    $args[] = $a;
                } elseif (is_string($a)) {
                    $args[] = '\'' . RaiseHtml::encode($a) . '\'';
                } elseif (is_array($a)) {
                    $tmp = RaiseHtml::encode(var_export($a, true));
                    $ext = substr($tmp, 8, strlen($tmp) - 9);
                    $args[] = 'array(' . ($ext ? '<div>' . $ext . '</div>' : '') . ')';
                } elseif (is_object($a)) {
                    $args[] = '<i>' . get_class($a) . '</i>';
                } else {
                    $args[] = RaiseHtml::encode(var_export($a, true));
                }
            }
            $args = implode(', ', $args);
            $line = '';
            if(array_key_exists('line', $t)){
                $line = '[Line ' . $t['line'] . '] ';
            }
            $strTrace .= '<div class="st"><div class="fl">'.$line . $t['file'] . '</div><div>' . (array_key_exists('class', $t) ? $t['class'] . $t['type'] . '<b>' . $t['function'] . '</b>' : '<b>' . $t['function'] . '</b>') . '(' . $args . ');</div></div>';
        }
        $this->template()->fields()->add('default', '<h1>Application Exception</h1>
            <div class="msg">Your request could not be delivered due to an unexpected error in the application.</div>
            <div class="s">Exception Message</div>
            <pre>' . $this->exception->getCode() . ': ' . $this->exception->getMessage() . '</pre>
            <div class="s">Stack Trace</div>
            <pre>' . $strTrace . '</pre>
        ');
    }

}